<?php
/**
 * Zeed Platform Project
 * Based on Zeed Framework & Zend Framework.
 *
 * LICENSE
 * http://www.zeed.com.cn/license/
 *
 * @category Zeed
 * @package Zeed_ChangeMe
 * @subpackage ChangeMe
 * @copyright Copyright (c) 2010 Zeed Technologies PRC Inc. (http://www.zeed.com.cn)
 * @author Zeed Team (http://blog.zeed.com.cn)
 * @since 2011-10-26
 * @version SVN: $Id$
 */
class Trend_Attachment_AudioIgnoreBlank
{
    /**
     * 音频跳过空白
     *
     * @param string $filepath 音频文件地址
     * @return array
     */
    public static function run($filepath)
    {
        $res = array('status' => 0, 'error' => null, 'data' => null);
        
        /* 处理需要处理的文件地址 */
        $input = ZEED_PATH_UPLOAD . $filepath;
        $input = str_replace('//', '/', $input);
        
        /* 处理输出路径 */
        $timenow = Util_Timer::getMicrotime();
        $filepath_arr = explode('.', $filepath);
        $filepath_arr[count($filepath_arr) - 2] .= '_' . $timenow;
        $filepath_arr[count($filepath_arr) - 1] = 'mp3';
        $filepath_new = implode('.', $filepath_arr);
        $output = ZEED_PATH_UPLOAD . $filepath_new;
        $output = str_replace('//', '/', $output);
        
        /* 执行 */
        $exec = "ffmpeg -i {$input} -af silenceremove=start_periods=1:start_threshold=-30dB:stop_periods=-1:stop_threshold=-50dB:start_silence=0.2:stop_silence=1 {$output} -y && echo success";
        $result = exec($exec);
        if ($result != 'success') {
            $res['status'] = 1;
            $res['error'] = '请检查磁盘读写权限';
        } else {
            $res['data']['filepath'] = $filepath_new;
        }
        
        return $res;
    }
}

// End ^ Native EOL ^ UTF-8